package com.gy.smartmanagement.base.activity;

import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.google.android.material.snackbar.Snackbar;
import com.gy.smartmanagement.base.app.AppApplication;
import com.gy.smartmanagement.dialog.LoadingDialogFragment;
import com.gy.smartmanagement.utils.ActivityUtils;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentManager;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * activity基类
 */

public abstract class BaseActivity extends AppCompatActivity {
  protected Unbinder unbinder;
  protected AppApplication application;
  protected FragmentManager fragmentManager;
  protected LoadingDialogFragment loadingDialogFragment;

  @Override
  protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (getLayoutResId() != 0) {
      setContentView(getLayoutResId());
    }

    application = (AppApplication) getApplication();
    fragmentManager = getSupportFragmentManager();
    loadingDialogFragment = LoadingDialogFragment.newInstance();
    unbinder = ButterKnife.bind(this);
    ActivityUtils.addActivity(this.getClass().getSimpleName(), this);
  }


  protected abstract int getLayoutResId();

  protected View getRootView() {
    return ((ViewGroup) findViewById(android.R.id.content)).getChildAt(0);
  }



  public void showToastTip(int resId) {
    Toast.makeText(this, getString(resId), Toast.LENGTH_SHORT).show();
  }

  public void showToastTip(String message) {
    Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
  }

  public void showSnackBarTip(String message) {
    Snackbar.make(getRootView(), message, Snackbar.LENGTH_SHORT).show();
  }

  public void showSnackBarTip(int resId) {
    Snackbar.make(getRootView(), resId, Snackbar.LENGTH_SHORT).show();
  }

  public void showLoading(FragmentManager manager) {
    loadingDialogFragment.show(manager, LoadingDialogFragment.TAG);
  }

  public void showLoadingWithStatus(FragmentManager manager, String status) {
    loadingDialogFragment.showWithStatus(manager, status);
  }

  public void hideLoading() {
    loadingDialogFragment.dismiss();
  }



  @Override
  protected void onDestroy() {
    super.onDestroy();
    ActivityUtils.removeActivity(this.getClass().getSimpleName());
    if (unbinder != null) {
      unbinder.unbind();
      unbinder = null;
    }
  }


}
